package com.atguigu.rabbitmq.seven;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: EmitLogTopic
 * Description: 生产者
 * Date: 2023/04/28 PM 3:25:18
 * <p>
 * project: atguigu-rabbitmq
 * package: com.atguigu.rabbitmq.seven
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class EmitLogTopic {

    /**
     * 交换机的名称
     */
    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();

        /*
          Q1–>绑定的是
          中间带 orange 带 3 个单词的字符串(.orange.)
          Q2–>绑定的是
          最后一个单词是 rabbit 的 3 个单词(..rabbit)
          第一个单词是 lazy 的多个单词(lazy.#)
         */
        Map<String, String> bindingKeyMap = new HashMap<>(8);
        bindingKeyMap.put("quick.orange.rabbit", "被队列Q1Q2接收到");
        bindingKeyMap.put("lazy.orange.elephant", "被队列Q1Q2接收到");
        bindingKeyMap.put("quick.orange.fox", "被队列Q1接收到");
        bindingKeyMap.put("lazy.brown.fox", "被队列Q2接收到");
        bindingKeyMap.put("lazy.pink.rabbit", "虽然满足两个绑定但只被Q2接收一次");
        bindingKeyMap.put("quick.brown.fox", "不匹配任何绑定不会被任何队列接收到会被丢弃");
        bindingKeyMap.put("quick.orange.male.rabbit", "是四个单词不匹配任何绑定会被丢弃");
        bindingKeyMap.put("lazy.orange.male.rabbit", "是四个单词但匹配Q2");

        bindingKeyMap.forEach((routingKey, message) -> {
            try {
                channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println("生产者发出消息：" + message);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
